package cn.java71.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
//>>>@WebServlet(name = "hello2",value = {"/sayhi","nihao","/goutoumoringi"})
 //>>>@WebServlet("/fff")
public class HelloServlet extends HttpServlet {
    //get 和post
    public HelloServlet(){
        System.out.println("HelloServlet 构造了。。。");
    }
    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("HelloServlet 初始化。。。");
    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("HelloServlet 被销毁了。。。");
    }
        @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //ServletContext sc = super.getServletContext();
            //sc.getRealPath("");
            ServletContext sc2=req.getSession().getServletContext();
            //>sc2.getRealPath("")
            //req.getRequestDispatcher("/xx.jsp").forward();
            System.out.println("准备服务。。。");
            super.service(req, resp);
            System.out.println("服务结束。。。");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入了get");
        //req.setCharacterEncoding("utf-8");
        //响应字符集
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.write("<html>");
        out.write("<body>");
        out.write("<h1 style='color:red;font-size:90px'>");
        out.print("This is GET!");
        out.write("欢迎来到servlet的世界，现在时刻:"+new Date());
        out.write("</h1>");
        out.write("</body>");
        out.write("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入了post");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.write("<html>");
        out.write("<body>");
        out.write("<h1 style='color:red;font-size:90px'>");
        out.print("This is Post!");
        out.write("欢迎来到servlet的世界，现在时刻:"+new Date());
        out.write("</h1>");
        out.write("</body>");
        out.write("</html>");
    }
}
